/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#include "UGame/Util/Time.h"
#include <cassert>
#include "UGame/Platform.h"
#include "UGame/Macros.h"
#if defined(PLATFORM_APPLE)
#include <mach/clock.h>
#include <mach/mach.h>
#endif // _APPLE


namespace UGame {
namespace Util {

    struct tm* localtime(struct tm* _Tm, time_t* _Time)
    {
        UASSERT(_Tm != NULL && _Time != NULL);
        *_Tm = *::localtime(_Time);
        return _Tm;
    }

    time_t FromStartup(void)
    {
    	struct timespec _Timespec;
#if defined(PLATFORM_LINUX)
        clock_gettime(CLOCK_MONOTONIC, &_Timespec);
#elif defined(PLATFORM_APPLE)
        clock_serv_t _Cclock;
        mach_timespec_t _Mts;
        host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &_Cclock);
        clock_get_time(_Cclock, &_Mts);
        mach_port_deallocate(mach_task_self(), _Cclock);
        _Timespec.tv_sec = _Mts.tv_sec;
        _Timespec.tv_nsec = _Mts.tv_nsec;
#endif
    	return time_t(_Timespec.tv_sec * 1000 + _Timespec.tv_nsec / 1000000);
    }

}}
